/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using WIA;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;

namespace MyPhotoIndex.Importer.CameraImageAcquisition
{
    public class DigitalCamera : ImagingDevice
    {
        private DeviceInfoClass m_devicInfo;
        private int             m_cameraImageCount;

        internal DigitalCamera(DeviceInfoClass devicInfo)
        {
            m_devicInfo = devicInfo;
            foreach (Property p in m_devicInfo.Properties)
            {
                PropertyClass deviceInfoClass = (PropertyClass)Marshal.CreateWrapperOfType((IProperty)p, typeof(PropertyClass));
                string propertyName = deviceInfoClass.Name;
                object value = deviceInfoClass.get_Value();
                Debug.Print(propertyName);          
                string stringValue = string.Empty; 
                try
                {
                    if (value != null &&
                        value is string)
                    {
                        stringValue = (string)value;
                    }
                }
                catch { }

                switch (propertyName)
                {
                    case "Manufacturer":
                        Manufacturer = stringValue;
                        break;

                    case "Description":
                        Description = stringValue;
                        break;

                    case "Name":
                        Name = stringValue;
                        break;
                }
            }
        }

        public override void DownloadImages()
        {
            ImageList.Clear();

            Device device = m_devicInfo.Connect();
            m_cameraImageCount = device.Items.Count;
            DeviceClass deviceClass = (DeviceClass)Marshal.CreateWrapperOfType((IDevice)device, typeof(DeviceClass));
            ItemsClass itemsClass = (ItemsClass)Marshal.CreateWrapperOfType((IItems)deviceClass.Items, typeof(ItemsClass));

            GetImages(ImageList, itemsClass);
        }

        internal void GetImages(List<IAcquiredImage> imageList, ItemsClass items)
        {
            foreach (Item item in items)
            {
                ItemClass itemClass = (ItemClass)Marshal.CreateWrapperOfType((IItem)item, typeof(ItemClass));
                if (itemClass.Items.Count == 0)
                {
                    DigitalCameraImage image = new DigitalCameraImage(itemClass);
                    if (string.IsNullOrEmpty(image.FileExtention) == false)
                    {
                        imageList.Add(image);
                        base.InvokeThumbnailDownloadEvent(new ImagingDeviceImageTransferEventArgs(m_cameraImageCount, ImageList.Count, image.Name));
                    }
                }

                ItemsClass itemsClass = (ItemsClass)Marshal.CreateWrapperOfType((IItems)item.Items, typeof(ItemsClass));
                GetImages(imageList, itemsClass);
            }
        }
    }
}
